<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 100px;
            height: 100px;
            background-color: blue;
            position: absolute;
        }
    </style>
</head>

<body>
    <div></div>
    <script>
        var box = document.querySelector("div")
        box.onmousedown = function (e) {
            var e = e || window.event
            var x = e.clientX - this.offsetLeft
            var y = e.clientY - this.offsetTop
            document.onmousemove = function (e) {
                var e = e || window.event
                var xx = e.clientX
                var yy = e.clientY
                box.style.top = yy
                box.style.left = xx
            }
            document.onmouseup = function () {
                document.onmousemove = ""
            }
        }
    </script>
</body>

</html>